<template>
  <div class="driver-container">
    <div class="header">
      <!-- <el-page-header content="详情页面" @back="goBack">
        h
      </el-page-header> -->
    </div>
    <div class="footer">
      <el-card>
        <div class="card-header">
          <el-tabs v-model="activeName" @tab-click="tabClick">
            <el-tab-pane label="基本信息" name="information">
              <div class="card-footer">
                <div>
                  <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="auto"
                  >
                    <el-row type="flex" justify="space-between">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="userId"
                        label="员工编号："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="form.userId"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            disabled
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="name"
                        label="司机名称："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="form.name"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            disabled
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="agency.name"
                        label="所属机构："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="form.agency.name"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            disabled
                            placeholder=""
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="start">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="mobile"
                        label="电话："
                      >
                        <el-col>
                          <el-input
                            v-model="form.mobile"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            disabled
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="allowableLoad"
                        label="年龄："
                        style="padding-left: 24px"
                      >
                        <el-col>
                          <el-input
                            v-model="form.age"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="center">
                      <el-form-item
                        class="last-FormItem"
                        style="margin-top: 30px"
                      >
                        <template #default="{ row }">
                          <el-button
                            v-show="disabled"
                            type="success"
                            size="small"
                            class="buttonStyle"
                            @click="editClick(row)"
                          >编辑</el-button>
                          <el-button
                            v-show="!disabled"
                            size="small"
                            class="buttonStyle"
                            type="success"
                            @click="saveClick"
                          >保存</el-button>
                          <el-button
                            v-show="!disabled"
                            size="small"
                            class="buttonStyle"
                            type="success"
                            plain
                            @click="cancelClick"
                          >取消</el-button>
                        </template>
                      </el-form-item>
                    </el-row>
                  </el-form>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="驾驶证信息" name="drivingLicence">
              <div class="card-footer">
                <div>
                  <el-form
                    ref="form"
                    :model="driverForm"
                    :rules="rules"
                    label-width="auto"
                  >
                    <el-row type="flex" justify="space-between">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="licenseNumber"
                        label="驾驶证号："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.licenseNumber"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="allowableType"
                        label="准驾车型："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.allowableType"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="initialCertificateDate"
                        label="初次领证日期："
                      >
                        <el-col :span="8">
                          <el-date-picker
                            v-model="driverForm.initialCertificateDate"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                            type="date"
                            placeholder=""
                          />
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="space-between">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="validPeriod"
                        label="驾驶证有效期："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.validPeriod"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="driverAge"
                        label="驾龄："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.driverAge"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="licenseType"
                        label="驾驶证类型："
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.licenseType"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            placeholder=""
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="start">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="qualificationCertificate"
                        label="从业资格证："
                      >
                        <el-col :span="6">
                          <el-input
                            v-model="driverForm.qualificationCertificate"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            placeholder=""
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop="passCertificate"
                        label="入场证信息："
                        style="padding-left: 24px"
                      >
                        <el-col :span="8">
                          <el-input
                            v-model="driverForm.passCertificate"
                            clearable
                            :class="{ inputDisabled: disabled }"
                            :disabled="disabled"
                            placeholder=""
                            class="inputStyle"
                          />
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="start">
                      <el-form-item
                        :class="{ formItem: disabled }"
                        prop=""
                        label="图片信息："
                        style="width: 100%"
                      >
                        <el-col v-if="disabled">
                          <ul class="displayImg">
                            <li
                              v-for="(item, index) in imgList"
                              :key="item.url"
                            >
                              <img :src="item.url" alt="">
                              <i
                                class="el-icon-zoom-in icon"
                                @click="magnifyClick(index)"
                              />
                            </li>
                          </ul>
                        </el-col>
                        <el-col v-else>
                          <el-upload
                            action=""
                            :http-request="uploadImg"
                            list-type="picture-card"
                            :on-preview="handleImgPreview"
                            :on-remove="handleRemove"
                            :file-list="imgList"
                            class="upload-demo"
                          >
                            <div class="upload-box">
                              <i class="upload-icon" />
                              <span class="upload-text">上传图片</span>
                            </div>
                            <div slot="file" slot-scope="{ file }">
                              <img
                                class="el-upload-list__item-thumbnail uploadImg"
                                :src="file.url"
                                alt=""
                              >
                              <div class="iconBox">
                                <span class="el-upload-list__item-actions">
                                  <span
                                    class="el-upload-list__item-preview"
                                    @click="viewImg(file)"
                                  >
                                    <i class="el-icon-zoom-in" />
                                  </span>
                                  <span
                                    v-show="!disabled"
                                    class="el-upload-list__item-delete"
                                    @click="handleRemove(file)"
                                  >
                                    <i class="el-icon-delete" />
                                  </span>
                                </span>
                              </div>
                            </div>
                          </el-upload>
                          <span
                            v-show="!disabled"
                            class="prompt-text"
                          >注：图片大小不超过5M；仅能上传 PNG JPEG
                            JPG类型图片；建议上传400*300尺寸的图片；最多上传2张</span>
                        </el-col>
                      </el-form-item>
                    </el-row>
                    <el-row type="flex" justify="center">
                      <el-form-item class="last-FormItem">
                        <template>
                          <el-button
                            v-show="disabled"
                            type="success"
                            size="small"
                            class="buttonStyle"
                            @click="editClick"
                          >编辑</el-button>
                          <el-button
                            v-show="!disabled"
                            size="small"
                            class="buttonStyle"
                            type="success"
                            @click="saveClick"
                          >保存</el-button>
                          <el-button
                            v-show="!disabled"
                            size="small"
                            class="buttonStyle"
                            type="success"
                            plain
                            @click="cancelClick"
                          >取消</el-button>
                        </template>
                      </el-form-item>
                    </el-row>
                  </el-form>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
    </div>
    <!-- 图片展示 -->
    <el-dialog :visible.sync="imgdialogVisible" width="35%">
      <div class="dialogBox">
        <img width="100%" :src="imgUrl" alt="" style="vertical-align: bottom">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDriverDetailAPI,
  getDriverLicenseAPI,
  saveDriverAPI
} from '@/api/employee'
export default {
  data() {
    return {
      userId: this.$route.params.id,
      disabled: true,
      activeName: 'information',
      // 图片展示
      imgdialogVisible: false,
      // 基本信息
      form: {
        age: 0,
        avatar: '',
        mobile: '',
        name: '',
        userId: 0,
        workNumber: '',
        workStatus: 0,
        agency: {
          name: ''
        }
      },
      // 驾驶证信息
      driverForm: {},
      rules: {},
      vehiceTypeList: [],
      // 图片信息
      imgList: [],
      imgUrl: ''
    }
  },
  created() {
    this.detailDataList()
  },
  methods: {
    // 获取基本信息
    async detailDataList() {
      const { data } = await getDriverDetailAPI(this.userId)
      this.form = data.data
    },
    // 获取驾驶证信息
    async driverLicense() {
      const { data } = await getDriverLicenseAPI(this.userId)
      this.driverForm = data.data
      const result = data.data.picture.split(',')
      this.imgList = result.map((item) => {
        return {
          url: item
        }
      })
    },
    // tab切换
    tabClick(key) {
      if (key.name === 'information') {
        this.detailDataList()
      } else {
        this.driverLicense()
      }
      this.disabled = true
    },
    // 编辑
    editClick() {
      this.activeName === 'information'
        ? this.detailDataList()
        : this.driverLicense()
      this.disabled = false
    },
    // 保存
    async saveClick() {
      await saveDriverAPI(this.userId, this.form)
      this.disabled = true
    },
    // 取消
    cancelClick() {
      this.disabled = true
    },
    // 图片展示
    magnifyClick(index) {
      this.imgUrl = this.imgList[index].url
      this.imgdialogVisible = true
    },
    uploadImg() {},
    handleRemove() {},
    handleImgPreview() {}
  }
}
</script>

<style lang="scss" scoped>
.footer {
  padding: 10px 20px;
  .card-footer {
    padding: 10px 40px;
    .el-form-item {
      padding: 0 20px;
      width: 380px;

      ::v-deep .el-form-item__label {
        font-size: 13px;
      }
      .inputStyle {
        width: 220px;
      }
      .inputDisabled {
        ::v-deep .el-input__inner {
          border: none !important;
          color: #000 !important;
          font-size: 13px !important;
          background-color: transparent !important;
        }
      }

      .upload-demo {
        .uploadImg {
          width: 210px;
          height: 146px;
          border-radius: 6px;
        }
        .upload-icon {
          margin: 0 auto;
          transform: translateY(80%);
          position: relative;
          display: flex;
          justify-content: center;
          align-items: center;
          width: 50px;
          height: 50px;
          &::before {
            content: "";
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 25px;
            height: 25px;
            background: url("../../../assets/icon_upload@2x.png") no-repeat;
            background-size: 25px;
          }
        }
        .upload-text {
          display: inline-block;
          transform: translateY(-150%);
          line-height: 1.5;
          color: #8c939d;
          font-size: 13px;
        }
      }
    }
    .prompt-text {
      display: inline-block;
      margin: 5px 0;
      line-height: 1.5;
      color: #8c939d;
      font-size: 12px;
    }
    .formItem {
      margin: 0;

      .displayImg {
        list-style: none;
        width: 100%;
        height: 100%;
        li {
          position: relative;
          display: inline-block;
          margin-left: 10px;
          width: 210px;
          height: 146px;
          border-radius: 6px;
          border: 1px solid #c0ccda;

          img {
            width: 100%;
            height: 100%;
            border-radius: 6px;
          }

          .icon {
            position: absolute;
            top: 50%;
            left: 50%;
            z-index: 20;
            transform: translate(-50%, -50%);
            font-size: 20px;
            color: #fff;
            visibility: hidden;
            transition: opacity 0.3s;
          }
          &::after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            width: 210px;
            height: 146px;
            z-index: 11;
            transition: background-color 0.3s;
          }

          &:hover {
            &::after {
              transition: background-color 0.3s;
              border-radius: 6px;
              background-color: rgba(0, 0, 0, 0.4);
            }
            & .icon {
              visibility: visible;
              transition: opacity 0.3s;
              opacity: 0.8;
            }
          }
        }
      }
    }
    .last-FormItem {
      margin: 0;
      ::v-deep .el-form-item__content {
        margin: 0;
        text-align: center;
      }
      .buttonStyle {
        width: 80px;
        height: 30px;
        transform: translateX(-50%);

        &.buttonStyle:first-child {
          background-color: #189450;
          border-color: #189450;
          margin-right: 10px;

          &:hover {
            opacity: 0.7;
          }
        }
      }
    }
    .dialogBox {
      float: left;
      width: 100%;
      height: 100%;
      text-align: center;
    }
  }
}
// tab栏样式
::v-deep .el-tabs--top .el-tabs__item.is-top:nth-child(2) {
  padding-left: 20px;
}
::v-deep .el-tabs__active-bar {
  height: 3px;
  background-color: #189450;
}
::v-deep .el-tabs__item.is-active {
  color: #189450;
}
::v-deep .el-tabs__item {
  font-size: 14px;
  font-weight: bold;
}
// 文件上传样式
::v-deep .el-upload-list__item-actions {
  width: 210px;
}
::v-deep .el-upload--picture-card {
  width: 210.22px;
  &:hover .upload-icon::before,
  &:hover span {
    color: #189450;
  }
  &:hover {
    border-color: #189450;
  }
}
::v-deep .el-upload-list--picture-card .el-upload-list__item {
  width: 210.22px;
}
// 弹窗样式
::v-deep .el-dialog__header {
  padding: 0;
}
::v-deep .el-dialog__body {
  padding: 0;
}
</style>
